package com.novabluetooth.services.servicedevices;

import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.health.tools.ArithmeticUtils;
import com.novabluetooth.services.TJBluetoothLeService;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.UUID;

/**
 * 家康体温计
 *
 * @author nova
 */
public class TiWenJKService extends TJBluetoothLeService {
    /**
     * 家康额温枪
     */
    public final static UUID serviceJKUUID = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
    public final static UUID readCharactJKUUID = UUID.fromString("0000FFF2-0000-1000-8000-00805F9B34FB");

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void servicesDiscovered() {
        super.servicesDiscovered();
        BluetoothGattService gattService = mBluetoothGatt.getService(serviceJKUUID);
        readGattCharact = gattService.getCharacteristic(readCharactJKUUID);
        mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
        mBluetoothGatt.readCharacteristic(readGattCharact);
        List<BluetoothGattDescriptor> descriptors2 = readGattCharact.getDescriptors();
        for (BluetoothGattDescriptor dp1 : descriptors2) {
            dp1.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            Log.d(TAG, "writing enabledescriptor:" + mBluetoothGatt.writeDescriptor(dp1));
        }
    }

    @Override
    public boolean isRightData(byte[] buffer) {
        super.isRightData(buffer);
        String s = byteArray2HexString(buffer, buffer.length);
        Log.e("celiangshuju", "==================" + s);
        if (s.length() >= 16) {
            int data1 = Integer.parseInt(s.substring(8, 10) + "00", 16);//高位补00
            int data2 = Integer.parseInt(s.substring(10, 12), 16);
            Double data = (data1 + data2) / 100.0;
            DecimalFormat df = new DecimalFormat("#.0");
            df.setRoundingMode(RoundingMode.FLOOR);//此舍入模式始终不会增加计算值。
            String format = df.format(data);
            checkData = String.valueOf(format);
            Log.e("checkData", "checkData==========" + checkData);

            ifSuccess = true;
        } else {
            ifSuccess = false;
        }
        disconnect();
        return ifSuccess;
    }


    @Override
    public void closeDevice() {
        super.closeDevice();
    }


}
